package com.hst.capacity.application.biz.impl;

import com.hst.capacity.application.biz.AccessControlBiz;
import com.hst.capacity.domain.model.entity.auth.TokenEntity;
import com.hst.capacity.domain.model.entity.franchisee.FranchiseeEntity;
import com.hst.capacity.domain.model.entity.franchisee.FranchiseeUserEntity;
import com.hst.capacity.domain.model.entity.setting.station.SysRegionEntity;
import com.hst.capacity.domain.model.request.SignInParam;
import com.hst.capacity.domain.model.response.auth.LoginUser;
import com.hst.capacity.domain.model.response.auth.TokenVO;
import com.hst.capacity.domain.model.response.auth.UserBindingVO;
import com.hst.capacity.domain.service.FranchiseeUserService;
import com.hst.capacity.domain.service.TokenService;
import com.hst.capacity.domain.service.UserService;
import com.hst.capacity.infra.convertor.AuthConvertor;
import com.hst.capacity.infra.persistence.jpa.auth.UserRoleRepository;
import com.hst.capacity.infra.persistence.jpa.franchisee.FranchiseRepository;
import com.hst.capacity.infra.persistence.jpa.setting.station.SysRegionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccessControlBizImpl implements AccessControlBiz {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleRepository userRoleRepository;

    @Autowired
    private FranchiseeUserService franchiseeUserService;

    @Autowired
    private FranchiseRepository franchiseRepository;

    @Autowired
    private SysRegionRepository regionRepository;

    @Override
    public TokenVO signIn(SignInParam signInParam) {
        // 1 按账号、密码身份验证
        LoginUser user = userService.authenticate(signInParam.getUsername(), signInParam.getPassword());

        // 2 验证通过后创建Token并返回
        return createToken(user.getUser().getId());
    }

    @Override
    public UserBindingVO getUserBindingInfo(String userId) {

        // 1 查询运力商用户实体
        FranchiseeUserEntity franchiseeUserEntity = franchiseeUserService.getFranchiseeUser(userId);

        // 2 查询运力商实体
        FranchiseeEntity franchiseeEntity = franchiseRepository.findById(franchiseeUserEntity.getIdFranchisee()).get();

        //3 查找城市名
        SysRegionEntity byCode = regionRepository.findByCode(franchiseeEntity.getRegionCode());

        UserBindingVO bindingVO = AuthConvertor.INSTANCE.convert(franchiseeUserEntity, franchiseeEntity, byCode);

        //4 查找用户角色
        List<String> roles = userRoleRepository.findByUserId(userId);
        bindingVO.setRoles(roles);

        return bindingVO;
    }

    /**
     * 用户登出
     *
     * @param userId
     */
    @Override
    public String logout(String userId) {
        tokenService.destroyAccessToken(userId);
        return "OK";
    }

    private TokenVO createToken(String userId) {
        // 创建访问令牌
        TokenEntity tokenEntity = tokenService.createAccessToken(userId);
        return AuthConvertor.INSTANCE.convert(tokenEntity);
    }

}
